﻿using Microsoft.EntityFrameworkCore;
using MiniErp.Model.Entities.AssetManagement;
using MiniErp.Mysql.Data;
using MiniErp.Repository.IRepository.IAssetManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MiniErp.Repository.Repository.AssetManagement
{
    public class AssetInfoRepository : IAssetInfoRepository
    {
        private readonly MysqlContext _context;

        public AssetInfoRepository(MysqlContext context)
        {
            _context = context;
        }

        public async Task<int> AddAssetInfoAsync(AssetInfoEntity userinfo)
        {
            _context.AssetInfos.Add(userinfo);
            return await _context.SaveChangesAsync();
        }

        public async Task<List<AssetInfoEntity>> GetAllAssetInfoAsync(int Page, int num)
        {
            return await _context.AssetInfos
                .OrderBy(item => item.GeneralTypeId)
                .ThenBy(item => item.SecondaryTypeId)
                .ThenBy(item => item.AId)
                .Skip(num)
                .Take(Page)
                .ToListAsync();
        }

        public async Task<List<AssetInfoEntity>> GetFuzzyAssetInfoAsync(string value, int Page, int num)
        {
            return await _context.AssetInfos
                .Where(item => item.GeneralTypeId.ToString().Contains(value)
                               || item.SecondaryTypeId.ToString().Contains(value)
                               || item.AId.ToString().Contains(value)
                               || item.AssetName.Contains(value)
                               || item.Remarks.Contains(value)
                               || item.Position.PositionName.Contains(value)
                               || item.GeneralType.GeneralTypeName.Contains(value)
                               || item.SecondaryType.SecondaryTypeName.Contains(value)
                               || item.AssetState.AssetStateName.Contains(value)
                               || item.Department.DepartmentName.Contains(value)
                               || item.UserInfo.UserName.Contains(value))
                 .OrderBy(item => item.AId)
                 .ThenBy(item => item.SecondaryTypeId)
                 .ThenBy(item => item.GeneralTypeId)
                 .Skip(num)
                 .Take(Page)
                 .ToListAsync();
        }


    }
}
